package com.ph.shopping.facade.member.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.duboresult.DubboResult;
import com.ph.shopping.facade.member.service.ISmsCodeSendService;
import com.ph.shopping.facade.member.service.dbservice.facade.ISmsSendCodeDbService;

/**
 * 
 * @ClassName: SmsSendCodeService
 * @Description: 短信操作
 * @author tony
 * @date 2017年3月14日
 *
 */
@Component
@Service(version = "1.0.0")
public class SmsSendCodeService implements ISmsCodeSendService {

	private static final Logger log = LoggerFactory.getLogger(SmsSendCodeService.class);

	@Autowired
	private ISmsSendCodeDbService smsSendCodeDbService;

	@Override
	public Result getSmsCodeInfoByMobile(String mobile) {
		log.info("获取发送短息信息参数,mobile >> " + mobile);
		Result result = DubboResult.getDefResult();
		try {
			if (StringUtils.isNotBlank(mobile)) {
				result = smsSendCodeDbService.querySmsCodeInfoByMobile(mobile);
			}
		} catch (Exception e) {
			log.error("根据手机号：" + mobile + "查询短信相关信息错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result sendSmsCodeByNoMsg(String phone, String type) {
		log.info("发送短息信息参数,phone >> " + phone + " >> type >>" + type);
		Result result = DubboResult.getDefResult();
		try {
			if (StringUtils.isNotBlank(phone) && StringUtils.isNotBlank(type)) {
				result = smsSendCodeDbService.sendSmsCodeByNoMsg(phone, type);
			}
		} catch (Exception e) {
			result = DubboResult.getResultByError(result);
			log.error("发送短信错误", e);
		}
		return result;
	}

	@Override
	public Result sendSmsCodeByCustomMsg(String phone, String type, String message) {
		log.info("发送短息信息参数,phone >> " + phone + " >> type >>" + type + " >> message >>" + message);
		Result result = DubboResult.getDefResult();
		try {
			if (StringUtils.isNotBlank(phone) && StringUtils.isNotBlank(message)) {
				result = smsSendCodeDbService.sendSmsCodeByCustomMsg(phone, type, message);
			}
		} catch (Exception e) {
			result = DubboResult.getResultByError(result);
			log.error("发送自定义短信信息错误", e);
		}
		return result;
	}

	@Override
	public Result verifySmsCode(String phone, String type, String code) {
		log.info("验证短信验证码参数,phone >> " + phone + " >> type >>" + type + " >> code >>" + code);
		Result result = DubboResult.getDefResult();
		try {
			if (StringUtils.isNotBlank(phone) && StringUtils.isNotBlank(type) && StringUtils.isNotBlank(code)) {
				result = smsSendCodeDbService.verifySmsCode(phone, type, code);
			}
		} catch (Exception e) {
			log.error("验证短信验证码信息错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

}
